#!/bin/bash
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
source ./pkglib.sh || exit 3

if [ -z $1 ]; then
	errcho "Missing argument 1 (package group)";
	exit 3;
fi

GROUP=$1
if [ ! -z $2 ]; then
	CHAIN_OFFSET_PKG=$(pkgname $GROUP $2); if (($? != 0)); then exit 4; fi
else
	CHAIN_OFFSET_PKG="";
fi

if [ ! -d ./$GROUP ]; then
	errcho "Unknown package group $GROUP...";
	exit 5;
fi
if [ ! -r ./$GROUP.chain ]; then
	errcho "$GROUP has no chain list...";
	exit 6;
fi

if [[ $GROUP != "tools" && $(whoami) != "root" ]]; then
	errcho "Login as user root... only group tool must be installed as none root";
	exit 1;
fi

mkdir -p $LFS/sources/pkg/log || exit 12;

N=0;
if [[ $3 == "remove" ]]; then
	RM_MODE="remove";
	PKG_LIST="$(tac ./$GROUP.chain | grep '^[^#]' | grep -v '^$')";
else
	PKG_LIST="$(cat ./$GROUP.chain | grep '^[^#]' | grep -v '^$')";
fi
IFS=$'\n';
for p in $PKG_LIST; do
	PACKAGE=$(pkgname $GROUP $(echo $p | cut -d' ' -f1)) && \
	VERSION=$(pkgversion $GROUP $PACKAGE);
	if (($? != 0)); then
		errcho "$0: aborted chain process at chain position $p";
		exit 7;
	fi
	MINVER=$VERSION;
	if (( $(echo $p | wc -w) >= 2 )); then
		MINVER=$(echo $p | cut -d' ' -f2);
	fi
	if [[ $PACKAGE != "" ]]; then
		if [[ $CHAIN_OFFSET_PKG == "" || $CHAIN_OFFSET_PKG == $PACKAGE ]]; then
			CHAIN_OFFSET_PKG="";
			LOGF="${GROUP}_$(printf "%3.3d" $N)_${PACKAGE}";
			echo "$0: processing package... $GROUP/$PACKAGE" | tee $LFS/sources/pkg/log/$LOGF.log;
			# check if version given by chain file
			versionmatch $MINVER $VERSION > /dev/null 2> /dev/null;
			RES=$?;
			echo "version check of package $PACKAGE: required $MINVER, available: $VERSION --> $RES" >> $LFS/sources/pkg/log/$LOGF.log;
			if ((RES!=0)) && ((RES!=10)); then
				errcho "$0: min. required version ($MINVER) of package $PACKAGE is not available ($VERSION)";
				exit 12;
			fi
			PKG_INFO="";
			INSTVERS="";
			# clean up package database to prevent access errors
			if [[ $GROUP != "tools" ]]; then
				PKG_INFO=$(./pkg.sh search $PACKAGE | grep -o "package found: ${PACKAGE}$");
				if [[ $PKG_INFO != "" ]]; then
                    INSTVERS="$(pkginstalled $PACKAGE)" || exit 12;
					echo "clean package $PACKAGE archive folder first" >> $LFS/sources/pkg/log/$LOGF.log;
					./pkg.sh clean $PACKAGE >> $LFS/sources/pkg/log/$LOGF.log;
					if (($? != 0)); then
						errcho "$0: aborted chain process at cleaning of package $PACKAGE";
						exit 10;
					fi
				fi
			fi
			if [[ $RM_MODE != "" && $INSTVERS == "" ]]; then
                echo "package $PACKAGE not installed, ignore it" >> $LFS/sources/pkg/log/$LOGF.log;
			elif [[ $PKG_INFO == "" ]]; then
				if [[ $RM_MODE == "" ]]; then
					echo "$0: make package $PACKAGE in version $VERSION";
					echo "package $PACKAGE not available, make it..." >> $LFS/sources/pkg/log/$LOGF.log;
					./makepkg.sh $GROUP $PACKAGE >> $LFS/sources/pkg/log/$LOGF.log;
					if (($? != 0)); then
						errcho "$0: aborted chain process at making package $GROUP/$PACKAGE";
						exit 8;
					else
						echo "package make process successful: $GROUP/$PACKAGE" >> $LFS/sources/pkg/log/$LOGF.log;
					fi
				fi
			else
				if [[ $RM_MODE != "" ]]; then
                    echo "Remove package $PACKAGE in version $VERSION";
                    ./pkg.sh remove $PACKAGE >> $LFS/sources/pkg/log/$LOGF.log;
                    if (($? != 0)); then
                        errcho "$0: aborted chain process at remove of package $PACKAGE ($VERSION)";
                        exit 11;
                    else
                        echo "package remove processed successful, $(./pkg.sh search $PACKAGE | grep -o 'installed:.*$')" >> $LFS/sources/pkg/log/$LOGF.log;
                    fi
				else
					echo "package $PACKAGE in version $VERSION available as archive... skip make process" >> $LFS/sources/pkg/log/$LOGF.log;
				fi
			fi
			if [[ $GROUP != "tools" && $RM_MODE == "" ]]; then
                if [[ $INSTVERS != "" ]]; then
                    echo "package $PACKAGE of group $GROUP already installed, ignore it" >> $LFS/sources/pkg/log/$LOGF.log;
                else
                    echo "$0: process install package..." >> $LFS/sources/pkg/log/$LOGF.log;
                    ./pkg.sh install $PACKAGE $VERSION;
                    if (($? != 0)); then
                        errcho "$0: aborted chain process at installation of package $PACKAGE ($VERSION)";
                        exit 9;
                    else
                        echo "package installation processed successful, $(./pkg.sh search $PACKAGE | grep -o 'archive:.*$')" >> $LFS/sources/pkg/log/$LOGF.log;
                    fi
                fi
			else
				echo "packages of group $GROUP already installed by make process, finished" >> $LFS/sources/pkg/log/$LOGF.log;
			fi
		fi
		N=$((N=N+1));
	fi
done

echo "Package chain finished successful"
